#define _CRT_SECURE_NO_WARNINGS
#include<vector>
#include<stack>
using namespace std;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
    
};
vector<int> preorderTraversal(TreeNode* root) {
    vector<int> v;
    stack<TreeNode*> s;
    if (root) {
        s.push(root);
    }
    while (!s.empty()) {
        TreeNode* top = s.top();
        s.pop();
        if (top->right)s.push(top->right);
        if (top->left)s.push(top->left);
        v.push_back(top->val);
    }
    return v;
}